home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / System / ScreenDaemon 1.2 / ScreenDaemon cdev / ScreenDaemon Control.cp < prev    next >
Text File  |  1996-05-07  |  19KB  |  614 lines

  1. /**********************************************************
  2.  *    Basic Black © 1993,1994 by Mason L. Bliss
  3.  *    All Rights Reserved
  4.  *    version 1.4.1
  5.  *
  6.  *    The important bits involve the use of the Gestalt manager
  7.  *    for cdev/INIT communication, to which the cdev mechanism
  8.  *    itself is irrelevant.
  9.  **********************************************************/
  10. #include <Folders.h>
  11. #include <GestaltEqu.h>
  12. #include "ScreenDaemon Control.h"
  13. #include "SD.h"
  14.  
  15. // prototypes for 2 required procedures
  16. long runable(void);
  17. TControlPanel *makeCDEV(short numItems,DialogPtr cp);
  18.  
  19.  
  20. // return 1 if we can run, otherwise 0
  21. long runable(void)
  22. {
  23.     return 1;
  24. }
  25.  
  26. // code that allocates our cdev
  27. TControlPanel *makeCDEV(short numItems,DialogPtr cp)
  28. {
  29.     return new SDcdev(numItems,cp);
  30. }
  31.  
  32. /**********************************************************
  33.  *    SDcdev constructor
  34.  **********************************************************/
  35. SDcdev::SDcdev(short numItems,DialogPtr cp) : TControlPanel(numItems,cp)
  36. {
  37. }
  38.  
  39. /**********************************************************
  40.  *    SDcdev destructor
  41.  **********************************************************/
  42. SDcdev::~SDcdev(void)
  43. {
  44. }
  45.  
  46. /**********************************************************
  47.  *    SDcdev::Init
  48.  *
  49.  *    This method initializes stuff.
  50.  **********************************************************/
  51. long SDcdev::Init(void)
  52. {
  53.     PrefStructureHandle    prefHandle;
  54.     ControlHandle        theControl;
  55.     short                controlValue, theResFile;
  56.     long                theLong, width, height;
  57.     Rect                box;
  58.     Str255                theString;
  59.     Boolean                usingPrefsFile;
  60.  
  61.     /* Initialize stuff */
  62.     inherited::Init();
  63.     
  64.     /* Load & Lock Picture Resources */
  65.     pictHandles[0] = GetResource('PICT', topLeftPict);
  66.     pictHandles[1] = GetResource('PICT', topRightPict);
  67.     pictHandles[2] = GetResource('PICT', botRightPict);
  68.     pictHandles[3] = GetResource('PICT', botLeftPict);
  69.     pictHandles[4] = GetResource('PICT', noArrowPict);
  70.     pictHandles[5] = GetResource('PICT', upArrowPict);
  71.     pictHandles[6] = GetResource('PICT', dnArrowPict);
  72.     pictHandles[7] = GetResource('PICT', grArrowPict);
  73.     HLock(pictHandles[0]);
  74.     HLock(pictHandles[1]);
  75.     HLock(pictHandles[2]);
  76.     HLock(pictHandles[3]);
  77.     HLock(pictHandles[4]);
  78.     HLock(pictHandles[5]);
  79.     HLock(pictHandles[6]);
  80.     HLock(pictHandles[7]);
  81.  
  82.     /* Load the preferences */
  83.     theResFile = CurResFile();
  84.     usingPrefsFile = OpenPrefsFile();
  85.  
  86.     prefHandle = (PrefStructureHandle) GetIndResource('PREF', 1);
  87.     if (ResError()) {
  88.         return 1;
  89.     }
  90.     
  91.     /* get the sleep corner info */
  92.     sleepNowRect = (**prefHandle).sleepNowCorner;
  93.     GetDialogItem(fDialog, fLastItem - initDev + sleepNow,
  94.                 &controlValue, (Handle *) &theControl, &box);
  95.     SetDItem(fDialog, fLastItem - initDev + sleepNow,
  96.                 controlValue, pictHandles[sleepNowRect], &box);
  97.     InsetRect(&box, 3, 3);
  98.     EraseRect(&box);
  99.     InsetRect(&box, -3, -3);
  100.     DrawPicture((PicHandle) pictHandles[sleepNowRect], &box);
  101.     
  102.     width  = ((box.right - box.left) / 2);
  103.     height = ((box.bottom - box.top) / 2);
  104.     
  105.     /* Set up some small rects */
  106.     smallSNRect[0] = box;
  107.     smallSNRect[0].right -= width;
  108.     smallSNRect[0].bottom -= height;
  109.     smallSNRect[1] = box;
  110.     smallSNRect[1].left += width;
  111.     smallSNRect[1].bottom -= height;
  112.     smallSNRect[2] = box;
  113.     smallSNRect[2].left += width;
  114.     smallSNRect[2].top += height;
  115.     smallSNRect[3] = box;
  116.     smallSNRect[3].right -= width;
  117.     smallSNRect[3].top += height;
  118.     
  119.     /* get the wake corner info */
  120.     neverSleepRect = (**prefHandle).sleepNeverCorner;
  121.     GetDItem(fDialog, fLastItem - initDev + neverSleep,
  122.                 &controlValue, (Handle *) &theControl, &box);
  123.     SetDItem(fDialog, fLastItem - initDev + neverSleep,
  124.                 controlValue, pictHandles[neverSleepRect], &box);
  125.     InsetRect(&box, 3, 3);
  126.     EraseRect(&box);
  127.     InsetRect(&box, -3, -3);
  128.     DrawPicture((PicHandle) pictHandles[neverSleepRect], &box);
  129.     
  130.     width  = ((box.right - box.left) / 2);
  131.     height = ((box.bottom - box.top) / 2);
  132.     
  133.     /* Set up more small rects */
  134.     smallNSRect[0] = box;
  135.     smallNSRect[0].right -= width;
  136.     smallNSRect[0].bottom -= height;
  137.     smallNSRect[1] = box;
  138.     smallNSRect[1].left += width;
  139.     smallNSRect[1].bottom -= height;
  140.     smallNSRect[2] = box;
  141.     smallNSRect[2].left += width;
  142.     smallNSRect[2].top += height;
  143.     smallNSRect[3] = box;
  144.     smallNSRect[3].right -= width;
  145.     smallNSRect[3].top += height;
  146.             
  147.     /* get the "screen saver on" checkbox */
  148.     GetDItem(fDialog, fLastItem - initDev + screenOn, &controlValue, (Handle *) &theControl, &box);
  149.     if ((**prefHandle).saverOn)
  150.         SetCtlValue(theControl, 1);
  151.     
  152.     /* get the "Show Startup Icon" checkbox */
  153.     GetDItem(fDialog, fLastItem - initDev + startupIcon, &controlValue, (Handle *) &theControl, &box);
  154.     if ((**prefHandle).showIcon)
  155.         SetCtlValue(theControl, 1);
  156.     
  157.     /* get the idle time edit text item */
  158.     GetDItem(fDialog, fLastItem - initDev + idleMins, &controlValue, (Handle *) &theControl, &box);
  159.     theLong = (long) (**prefHandle).idleTime;
  160.     NumToString(theLong, theString);
  161.     SetIText((Handle) theControl, theString);
  162.     SelIText(fDialog, fLastItem - initDev + idleMins, 0, 32767);
  163.     
  164.     GetDItem(fDialog, fLastItem - initDev + idleRect, &controlValue, (Handle *) &theControl, &box);
  165.     height = ((box.bottom - box.top) / 2);
  166.     smIdleRect = box;
  167.     smIdleRect.bottom -= height;
  168.     
  169.     /* Clean up */
  170.     ReleaseResource((Handle) prefHandle);
  171.     if (usingPrefsFile) {
  172.         CloseResFile(CurResFile());
  173.         UseResFile(theResFile);
  174.     }
  175.  
  176.     /* see if the idle time arrow needs to be grey */
  177.     if (Gestalt('sysv', &theLong) != noErr || theLong < 0x700) {
  178.         GetDItem(fDialog, fLastItem - initDev + idleRect,
  179.                         &controlValue, (Handle *) &theControl, &box);
  180.         SetDItem(fDialog, fLastItem - initDev + idleRect,
  181.                 controlValue, pictHandles[grArrow], &box);
  182.     }
  183.     
  184.     return 0;
  185. }
  186.  
  187.  
  188.  
  189. /**********************************************************
  190.  *    SDcdev::Idle
  191.  *
  192.  *    Fix the EditText items if needed.
  193.  **********************************************************/
  194. long SDcdev::Idle(void)
  195. {
  196.     ControlHandle    theControl;
  197.     short            controlValue;
  198.     long            theLong;
  199.     Rect            box;
  200.     Str255            theString;
  201.     
  202.     if (mustRemember) {
  203.         mustRemember = false;
  204.         /* fix the idle time information if needed */
  205.         GetDItem(fDialog, fLastItem - initDev + idleMins, &controlValue, (Handle *) &theControl, &box);
  206.         GetIText((Handle) theControl, theString);
  207.         StringToNum(theString, &theLong);
  208.         if (theLong < 1) {
  209.             SetIText((Handle) theControl, "\p1");
  210.             SelIText(fDialog, fLastItem - initDev + idleMins, 0, 32767);
  211.         } else if (theLong > 60) {
  212.             SetIText((Handle) theControl, "\p60");
  213.             SelIText(fDialog, fLastItem - initDev + idleMins, 0, 32767);
  214.         }
  215.         RememberValues();
  216.     }
  217.     
  218.     return 0;
  219. }
  220.  
  221.  
  222.  
  223. /**********************************************************
  224.  *    SDcdev::Close
  225.  *
  226.  *    This method releases the 'PICT' resources.
  227.  **********************************************************/
  228. long SDcdev::Close(void)
  229. {
  230.     short    i;
  231.     
  232.     for (i = 0; i < 7; ++i) {
  233.         HUnlock(pictHandles[i]);
  234.         ReleaseResource(pictHandles[i]);
  235.     }
  236.     
  237.     inherited::Close();
  238.     
  239.     return 0;
  240. }
  241.  
  242.  
  243.  
  244. /**********************************************************
  245.  *    SDcdev::KeyDown
  246.  *
  247.  *    This filters out invalid key presses and stuff.
  248.  **********************************************************/
  249. long SDcdev::KeyDown(short theKey)
  250. {
  251.     if (((theKey < '0') || (theKey > '9')) && (theKey != 8)
  252.                         && (theKey != 9) && ((theKey < 28) || (theKey > 31))) {
  253.         fEvent->what = nullEvent;
  254.         return 0;
  255.     }
  256.     
  257.     /* if we got this far, then we have a reasonably valid keystroke */
  258.     mustRemember = true;
  259.     
  260.     return 0;
  261. }
  262.  
  263.  
  264.  
  265. /**********************************************************
  266.  *    SDcdev::ItemHit
  267.  *
  268.  *    This handles dialog item hits.
  269.  **********************************************************/
  270. long SDcdev::ItemHit(short theItem)
  271. {
  272.     ControlHandle    theControl;
  273.     short            controlValue, initialRect;
  274.     Rect            box, save;
  275.     long            theLong, theTime = 0;
  276.     Str255            theString;
  277.     Boolean            drawOtherAsWell = false;
  278.  
  279.     GetDItem(fDialog, fLastItem - initDev + theItem, &controlValue, (Handle *) &theControl, &box);
  280.     switch (theItem) {
  281.  
  282.         case sleepNow:
  283.             initialRect = sleepNowRect;
  284.             GlobalToLocal(&fEvent->where);
  285.             if (PtInRect(fEvent->where, &smallSNRect[topLeftRect])) {
  286.                 if (neverSleepRect == topLeftRect) {
  287.                     neverSleepRect = sleepNowRect;
  288.                     drawOtherAsWell = true;
  289.                 }
  290.                 sleepNowRect = topLeftRect;
  291.             } else if (PtInRect(fEvent->where, &smallSNRect[topRightRect])) {
  292.                 if (neverSleepRect == topRightRect) {
  293.                     neverSleepRect = sleepNowRect;
  294.                     drawOtherAsWell = true;
  295.                 }
  296.                 sleepNowRect = topRightRect;
  297.             } else if (PtInRect(fEvent->where, &smallSNRect[botRightRect])) {
  298.                 if (neverSleepRect == botRightRect) {
  299.                     neverSleepRect = sleepNowRect;
  300.                     drawOtherAsWell = true;
  301.                 }
  302.                 sleepNowRect = botRightRect;
  303.             } else if (PtInRect(fEvent->where, &smallSNRect[botLeftRect])) {
  304.                 if (neverSleepRect == botLeftRect) {
  305.                     neverSleepRect = sleepNowRect;
  306.                     drawOtherAsWell = true;
  307.                 }
  308.                 sleepNowRect = botLeftRect;
  309.             } else { /* This can't happen. */
  310.                 SysBeep(5);
  311.                 break;
  312.             }
  313.             
  314.             if (initialRect == sleepNowRect)    /* Nothing changed */
  315.                 break;
  316.             
  317.             /* Draw the new picture */
  318.             SetDItem(fDialog, fLastItem - initDev + sleepNow,
  319.                     controlValue, pictHandles[sleepNowRect], &box);
  320.             InsetRect(&box, 3, 3);
  321.             EraseRect(&box);
  322.             InsetRect(&box, -3, -3);
  323.             DrawPicture((PicHandle) pictHandles[sleepNowRect], &box);
  324.             
  325.             if (drawOtherAsWell) {
  326.                 GetDItem(fDialog, fLastItem - initDev + neverSleep, &controlValue, (Handle *) &theControl, &box);
  327.                 SetDItem(fDialog, fLastItem - initDev + neverSleep, controlValue,
  328.                         pictHandles[neverSleepRect], &box);
  329.                 InsetRect(&box, 3, 3);
  330.                 EraseRect(&box);
  331.                 InsetRect(&box, -3, -3);
  332.                 DrawPicture((PicHandle) pictHandles[neverSleepRect], &box);
  333.             }
  334.             RememberValues();
  335.             break;
  336.             
  337.         case neverSleep:
  338.             initialRect = neverSleepRect;
  339.             GlobalToLocal(&fEvent->where);
  340.             if (PtInRect(fEvent->where, &smallNSRect[topLeftRect])) {
  341.                 if (sleepNowRect == topLeftRect) {
  342.                     sleepNowRect = neverSleepRect;
  343.                     drawOtherAsWell = true;
  344.                 }
  345.                 neverSleepRect = topLeftRect;
  346.             } else if (PtInRect(fEvent->where, &smallNSRect[topRightRect])) {
  347.                 if (sleepNowRect == topRightRect) {
  348.                     sleepNowRect = neverSleepRect;
  349.                     drawOtherAsWell = true;
  350.                 }
  351.                 neverSleepRect = topRightRect;
  352.             } else if (PtInRect(fEvent->where, &smallNSRect[botRightRect])) {
  353.                 if (sleepNowRect == botRightRect) {
  354.                     sleepNowRect = neverSleepRect;
  355.                     drawOtherAsWell = true;
  356.                 }
  357.                 neverSleepRect = botRightRect;
  358.             } else if (PtInRect(fEvent->where, &smallNSRect[botLeftRect])) {
  359.                 if (sleepNowRect == botLeftRect) {
  360.                     sleepNowRect = neverSleepRect;
  361.                     drawOtherAsWell = true;
  362.                 }
  363.                 neverSleepRect = botLeftRect;
  364.             } else {    /* This can't happen. */
  365.                 SysBeep(5);
  366.                 break;
  367.             }
  368.             
  369.             if (initialRect == neverSleepRect)    /* Nothing changed */
  370.                 break;
  371.  
  372.             /* Draw the new picture */
  373.             SetDItem(fDialog, fLastItem - initDev + neverSleep,
  374.                     controlValue, pictHandles[neverSleepRect], &box);
  375.             InsetRect(&box, 3, 3);
  376.             EraseRect(&box);
  377.             InsetRect(&box, -3, -3);
  378.             DrawPicture((PicHandle) pictHandles[neverSleepRect], &box);
  379.  
  380.             if (drawOtherAsWell) {
  381.                 GetDItem(fDialog, fLastItem - initDev + sleepNow, &controlValue, (Handle *) &theControl, &box);
  382.                 SetDItem(fDialog, fLastItem - initDev + sleepNow, controlValue,
  383.                         pictHandles[sleepNowRect], &box);
  384.                 InsetRect(&box, 3, 3);
  385.                 EraseRect(&box);
  386.                 InsetRect(&box, -3, -3);
  387.                 DrawPicture((PicHandle) pictHandles[sleepNowRect], &box);
  388.             }
  389.             RememberValues();
  390.             break;
  391.  
  392.         case idleRect:
  393.             if (Gestalt('sysv', &theLong) != noErr && theLong < 0x700)
  394.                 break;
  395.             save = box;
  396.             GlobalToLocal(&fEvent->where);
  397.             GetDItem(fDialog, fLastItem - initDev + idleMins,
  398.                             &controlValue, (Handle *) &theControl, &box);
  399.             GetIText((Handle) theControl, theString);
  400.             StringToNum(theString, &theLong);
  401.             theTime = LMGetTicks() - 16;
  402.             if (PtInRect(fEvent->where, &smIdleRect)) {            // higher!!!
  403.                 DrawPicture((PicHandle) pictHandles[upArrow], &save);
  404.                 SelIText(fDialog, fLastItem - initDev + idleMins, 0, 32767);
  405.                 do {
  406.                     SystemTask();
  407.                     if (LMGetTicks() - theTime > 15) {
  408.                         theTime = LMGetTicks();
  409.                         if (theLong < 10) {
  410.                             NumToString(theLong += 1, theString);
  411.                             SetIText((Handle) theControl, theString);
  412.                             if (theLong == 10)
  413.                                 SelIText(fDialog, fLastItem - initDev + idleMins, 0, 32767);
  414.                         } else if (theLong < 60) {
  415.                             NumToString(theLong = (theLong > 55 ? 60 : theLong + 5), theString);
  416.                             SetIText((Handle) theControl, theString);
  417.                         }
  418.                     }
  419.                 } while (Button());
  420.             } else {                                            // lower.
  421.                 DrawPicture((PicHandle) pictHandles[dnArrow], &save);
  422.                 SelIText(fDialog, fLastItem - initDev + idleMins, 0, 32767);
  423.                 do {
  424.                     SystemTask();
  425.                     if (LMGetTicks() - theTime > 15) {
  426.                         theTime = LMGetTicks();
  427.                         if (theLong > 10) {
  428.                             NumToString(theLong = (theLong < 15 ? 10 : theLong - 5), theString);
  429.                             SetIText((Handle) theControl, theString);
  430.                         } else if (theLong > 1) {
  431.                                 NumToString(theLong -= 1, theString);
  432.                                 SetIText((Handle) theControl, theString);
  433.                         }
  434.                     }
  435.                 } while (Button());
  436.             }
  437.             box = save;
  438.             box.left += 3;
  439.             box.right -= 3;
  440.             box.top += 3;
  441.             box.bottom -= 3;
  442.             EraseRect(&box);
  443.             DrawPicture((PicHandle) pictHandles[noArrow], &save);
  444.             RememberValues();
  445.             break;
  446.  
  447.         case screenOn:
  448.         case startupIcon:
  449.             controlValue = GetCtlValue(theControl);
  450.             controlValue = !controlValue;
  451.             SetCtlValue(theControl, controlValue);
  452.             RememberValues();
  453.             break;
  454.  
  455.         default:
  456.             break;
  457.     }
  458.     
  459.     return 0;
  460. }
  461.  
  462.  
  463.  
  464. /**********************************************************
  465.  *    SDcdev::RememberValues
  466.  *
  467.  *    This saves the information in the CNFG resource, and,
  468.  *    if the Basic Black INIT is loaded, sets the values of
  469.  *    its patch globals in memory.
  470.  **********************************************************/
  471. void SDcdev::RememberValues(void)
  472. {
  473.     PrefStructureHandle    prefHandle;
  474.     ControlHandle        theControl;
  475.     short                controlValue, VRefNum, theResFile, thePrefsFile;
  476.     long                theLong, result, directoryID;
  477.     Rect                box;
  478.     Str255                theString;
  479.     PatchGlobalsPtr        pgPtr;
  480.  
  481.     /* Load the preferences */
  482.     theResFile = CurResFile();
  483.     if (Gestalt(gestaltFindFolderAttr, &result) == noErr)
  484.         if (gestaltFindFolderPresent == 0)
  485.             if (FindFolder(kOnSystemDisk, kPreferencesFolderType, kDontCreateFolder,
  486.                                                         &VRefNum, &directoryID) == noErr)
  487.                 if ((thePrefsFile = HOpenResFile(VRefNum, directoryID,
  488.                                 "\pScreenDaemon Prefs", fsCurPerm)) != -1) {
  489.                     prefHandle = (PrefStructureHandle) GetIndResource('PREF', 1);
  490.                     if (ResError()) { /* fixme */
  491.                         /*Error(cdevResErr);*/
  492.                         return;
  493.                     }
  494.  
  495.  
  496.                     /* save the idle time information */
  497.                     GetDItem(fDialog, fLastItem - initDev + idleMins, &controlValue, (Handle *) &theControl, &box);
  498.                     GetIText((Handle) theControl, theString);
  499.                     StringToNum(theString, &theLong);
  500.                     theLong = (theLong <  1 ?  1 : theLong);
  501.                     theLong = (theLong > 60 ? 60 : theLong);
  502.                     NumToString(theLong, theString);
  503.                     (**prefHandle).idleTime = (short) theLong;
  504.  
  505.                     /* save the sleep corner information */
  506.                     (**prefHandle).sleepNowCorner = sleepNowRect;
  507.                     
  508.                     /* save the wake corner information */
  509.                     (**prefHandle).sleepNeverCorner = neverSleepRect;
  510.                 
  511.                     /* save the "screen saver on" information */
  512.                     GetDItem(fDialog, fLastItem - initDev + screenOn, &controlValue, (Handle *) &theControl, &box);
  513.                     (**prefHandle).saverOn = GetCtlValue(theControl);
  514.                 
  515.                     /* save the "Startup Icon" information */
  516.                     GetDItem(fDialog, fLastItem - initDev + startupIcon, &controlValue, (Handle *) &theControl, &box);
  517.                     (**prefHandle).showIcon = GetCtlValue(theControl);
  518.                 
  519.                     /* If the extension's installed, then update it's patch globals */
  520.                     if (Gestalt('sDmn', (long *) &pgPtr) == noErr) {
  521.                         if (pgPtr->pgVersion == (**prefHandle).version) {
  522.                             pgPtr->pgMustSleep = false;
  523.                             pgPtr->pgSleepRect = (**prefHandle).sleepNowCorner;
  524.                             pgPtr->pgWakeRect = (**prefHandle).sleepNeverCorner;
  525.                             pgPtr->pgIdleTicks = (long) (**prefHandle).idleTime * 3600;
  526.                             pgPtr->pgMustSave = (**prefHandle).saverOn;
  527.                         }
  528.                     }
  529.                 }
  530.  
  531.     /* Clean up */
  532.     ChangedResource((Handle) prefHandle);
  533.     WriteResource((Handle) prefHandle);
  534.     ReleaseResource((Handle) prefHandle);
  535.     CloseResFile(thePrefsFile);
  536.     UseResFile(theResFile);
  537.  
  538. }
  539.  
  540.  
  541.  
  542. /*********************************************************************
  543.  * OpenPrefsFile:
  544.  *
  545.  * Open up our prefs file. If there are problems, then create a new
  546.  * prefs file.
  547.  *
  548.  *********************************************************************/
  549. Boolean OpenPrefsFile()
  550. {
  551.     long    result, directoryID;
  552.     FInfo    fileInfo;
  553.     short    VRefNum, theResFile, thePrefsFile, resAttrs;
  554.     Boolean    prefsFileOpen = false;
  555.     Handle    prefHandle, tmplHandle;
  556.  
  557.     theResFile = CurResFile();
  558.     /* Read in our prefs, and do a sick amount of error checking (bleah!) */
  559.     if (Gestalt(gestaltFindFolderAttr, &result) == noErr) {
  560.          if (result & Bit(gestaltFindFolderPresent)) {
  561.             if (FindFolder(kOnSystemDisk, kPreferencesFolderType, kCreateFolder,
  562.                     &VRefNum, &directoryID) == noErr) {
  563.                 if ((thePrefsFile = HOpenResFile(VRefNum, directoryID,
  564.                                 "\pScreenDaemon Prefs", fsCurPerm)) != -1) {
  565.                     /* Cool. We opened the file! Twiddle our watch variable... */
  566.                     prefsFileOpen = true;
  567.                     
  568.                 } else {    /* No go. Try to create a new prefs file. */
  569.                 
  570.                     HCreateResFile(VRefNum, directoryID, "\pScreenDaemon Prefs");
  571.                     HGetFInfo(VRefNum, directoryID, "\pScreenDaemon Prefs", &fileInfo);
  572.                     fileInfo.fdType = 'pref';
  573.                     fileInfo.fdCreator = 'sDmn';
  574.                     HSetFInfo(VRefNum, directoryID, "\pScreenDaemon Prefs", &fileInfo);
  575.                     if ((thePrefsFile = HOpenResFile(VRefNum, directoryID,
  576.                                     "\pScreenDaemon Prefs", fsCurPerm)) != -1) {
  577.                         /* We opened up the new file okay. */
  578.                         prefsFileOpen = true;
  579.                         
  580.                         /* Get preferences resource */
  581.                         UseResFile(theResFile);
  582.                         prefHandle = GetIndResource('PREF', 1);
  583.                         resAttrs = GetResAttrs(prefHandle);
  584.                         DetachResource(prefHandle);
  585.                         
  586.                         /* Write preferences resource */
  587.                         UseResFile(thePrefsFile);
  588.                         AddResource(prefHandle, 'PREF', 128, "\pPrefs");
  589.                         SetResAttrs(prefHandle, resAttrs);
  590.                         ChangedResource(prefHandle);
  591.                         WriteResource(prefHandle);
  592.                         ReleaseResource(prefHandle);
  593.  
  594.                         /* Get template resource */
  595.                         UseResFile(theResFile);
  596.                         tmplHandle = GetIndResource('TMPL', 1);
  597.                         resAttrs = GetResAttrs(tmplHandle);
  598.                         DetachResource(tmplHandle);
  599.                         
  600.                         /* Write template resource */
  601.                         UseResFile(thePrefsFile);
  602.                         AddResource(tmplHandle, 'TMPL', 128, "\pPREF");
  603.                         SetResAttrs(tmplHandle, resAttrs);
  604.                         ChangedResource(tmplHandle);
  605.                         WriteResource(tmplHandle);
  606.                         ReleaseResource(tmplHandle);
  607.                     }
  608.                 }
  609.             }
  610.         }
  611.     }
  612.     
  613.     return prefsFileOpen;
  614. }